package view.friend
{
import core.model.FriendModel;
import core.model.GameModel;

import flash.display.MovieClip;

import framework.proxy.ComponentProxy;
import framework.view.components.button.ButtonProxy;
import framework.view.components.container.RepeaterProxy;

import values.friend.FriendValue;

/**
 * FriendListProxy
 * @author jliang
 * 
 */
public class FriendListProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function FriendListProxy()
	{
		super();
	}	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var repeater:RepeaterProxy;	
	private var prevPageBtn:ButtonProxy;
	private var prevEndBtn:ButtonProxy;
	private var nextPageBtn:ButtonProxy;
	private var nextEndBtn:ButtonProxy;
	
	private var headIndex:int = 0;
	
	[Inject]
	public var friendModel:FriendModel;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		repeater = new RepeaterProxy();
		repeater.renderProxyRef = FriendListItemRenderProxy;
		repeater.parse(skin["list"]);
		
		prevPageBtn = new ButtonProxy();
		prevPageBtn.parse(skin["prevPageBtn"]);
		prevPageBtn.onClick = clickToPrevPageHandler;
		
		prevEndBtn = new ButtonProxy();
		prevEndBtn.parse(skin["prevEndBtn"]);
		prevEndBtn.onClick = clickToPrevEndHandler;
		
		nextPageBtn = new ButtonProxy();
		nextPageBtn.parse(skin["nextPageBtn"]);
		nextPageBtn.onClick = clickToNextPageHandler;
		
		nextEndBtn = new ButtonProxy();
		nextEndBtn.parse(skin["nextEndBtn"]);
		nextEndBtn.onClick = clickToNextEndHandler;
		
		update();
	}	
	//==========================================================================
	//  Private methods
	//==========================================================================
	private function getFriendData():Vector.<FriendValue>
	{		
		var source:Vector.<FriendValue> = GameModel.instance.friendModel.friends;
		
		var pageCount:int = repeater.itemProxys.length;
		
		var data:Vector.<FriendValue> = source.slice(headIndex, headIndex + pageCount);
		if (data.length < pageCount)
		{
			var count:int = data.length;
			for (var i:int = 0; i < pageCount - count; i++)
			{
				data.push(null);
			}
		}
		return data;
	}
	
	private function update():void
	{
		repeater.data = getFriendData();
		
		var pageCount:int = repeater.itemProxys.length;
		var total:int = GameModel.instance.friendModel.friendCount;
		
		prevPageBtn.enabled = false;
		prevEndBtn.enabled = false;
		nextPageBtn.enabled = false;
		nextEndBtn.enabled = false;
		
		if (headIndex + pageCount < total)
		{
			nextPageBtn.enabled = true;
			nextEndBtn.enabled = true;
		}
		if (headIndex > 0)
		{
			prevPageBtn.enabled = true;
			prevEndBtn.enabled = true;
		}
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	private function clickToPrevPageHandler():void
	{
		headIndex -= repeater.itemProxys.length;
		if (headIndex < 0) 
		{
			headIndex = 0;
		}
		update();
	}
	
	private function clickToNextPageHandler():void
	{
		headIndex += repeater.itemProxys.length;
		update();
	}
	
	private function clickToPrevEndHandler():void
	{
		headIndex = 0;
		update();
	}
	
	private function clickToNextEndHandler():void
	{
		var pageCount:int = repeater.itemProxys.length;
		var total:int = GameModel.instance.friendModel.friendCount;
		var page:int = Math.ceil(total / pageCount);
		if (page == 0) page = 1;
		
		headIndex += (page - 1) * pageCount;
		update();
	}
}
}


